﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HA.MT3DMS.FileLayer
{
    /// <summary>
    /// 源汇混合子程序包
    /// </summary>
    public class SSMixingPack:Package
    {
        public SSMixingPack()
        {
            this._fileType = FileType.SSM;
            this.Fname = "mt3dms.ssm";
        }
        private string  _FWEL="T";//井选项的逻辑标识
        /// <summary>
        /// 井选项的逻辑标识
        /// </summary>
        public string FWEL
        {
            get { return _FWEL; }
            set { _FWEL = value; }
        }
        private string _FDRN = "F";//渠选项的逻辑标识
        /// <summary>
        /// 渠选项的逻辑标识
        /// </summary>
        public string FDRN
        {
            get { return _FDRN; }
            set { _FDRN = value; }
        }
        private string _FRCH = "F";//补给选项的逻辑标识
        /// <summary>
        /// 补给选项的逻辑标识
        /// </summary>
        public string FRCH
        {
            get { return _FRCH; }
            set { _FRCH = value; }
        }
        private string _FEVT = "F";//蒸散选项的逻辑标识
        /// <summary>
        /// 蒸散选项的逻辑标识
        /// </summary>
        public string FEVT
        {
            get { return _FEVT; }
            set { _FEVT = value; }
        }
        private string _FRIV = "F";//河流选项的逻辑标识
        /// <summary>
        /// 河流选项的逻辑标识
        /// </summary>
        public string FRIV
        {
            get { return _FRIV; }
            set { _FRIV = value; }
        }
        private string _FGHB = "F";//一般水头依赖边界条件选项的逻辑标识
        /// <summary>
        /// 一般水头依赖边界条件选项的逻辑标识
        /// </summary>
        public string FGHB
        {
            get { return _FGHB; }
            set { _FGHB = value; }
        }
        private int _FNEW;//保留给附加源汇选项的逻辑标识。如果这些选项中的任何一个在水流模型中使用，它所对应的逻辑标识必须被设为T（真），否则需被设为F（假）
        /// <summary>
        /// 保留给附加源汇选项的逻辑标识。
        /// 如果这些选项中的任何一个在水流模型中使用，它所对应的逻辑标识必须被设为T（真），否则需被设为F（假）
        /// </summary>
        public int FNEW
        {
            get { return _FNEW; }
            set { _FNEW = value; }
        }

        private int _MXSS;//水流模型中包含的点状源和汇的最大数量
        /// <summary>
        /// 水流模型中包含的点状源和汇的最大数量
        /// </summary>
        public int MXSS
        {
            get { return _MXSS; }
            set { _MXSS = value; }
        }

        List<int> _INCRCH = new List<int>();
        /// <summary>
        /// 如果FRCH=T,输入
        /// 在当前应力期是否读取每个物种的一个含有迁入量浓度的数组
        /// 如果INCRCH>=0,读取每个物种的一个含有迁入量浓度的数组
        /// 如果INCRCH0, 重复使用上个应力期的迁入量浓度.如果第一个应力期指定INCRCH0,
        /// 那么正迁入量(源)的浓度默认为0,并且负迁入量(汇)被默认设置等于含水层浓度
        /// </summary>
        public List<int> INCRCH
        {
            get { return _INCRCH; }
            set { _INCRCH = value; }
        }
    
     

    

        private List<List<double[,]>> _CRCH = new List<List<double[,]>>();//某个特定物种的迁入量浓度
        /// <summary>
        /// 对于每个物种,如果FRCH=T并且INCRCH>=0
        /// 某个特定物种的迁入量浓度 
        /// </summary>
        public List<List<double[,]>> CRCH
        {
            get { return _CRCH; }
            set { _CRCH = value; }
        }



        List<int> _INCEVT = new List<int>();
        /// <summary>
        /// 如果FEVT=T,输入
        /// 标示在当前应力期是否读取每个物种的一个含有土壤蒸发迁移量浓度的数组
        /// 如果INCEVT >=0,读取每个物种的一个含有土壤蒸发迁移量浓度的数组.
        /// 如果INCEVT0, 重复使用上个应力期的迁移量浓度.如果第一个应力期指定INCEVT 小于 0,
        /// 那么正土壤蒸发迁移量(源)的浓度默认为0,并且负土壤蒸发迁移量(汇)被默认设置等于含水层浓度
        /// </summary>
        public List<int> INCEVT
        {
            get { return _INCEVT; }
            set { _INCEVT = value; }
        }
       
      


        private List<List<double[,]>> _CEVT = new List<List<double[,]>>();//某个特定物种的土壤蒸发迁移量浓度
        /// <summary>
        /// 对于每个物种如果FEVT=T并且INCEVT>=0,输入
        /// 某个特定物种的土壤蒸发迁移量浓度       
        /// </summary>
        public List<List<double[,]>> CEVT
        {
            get { return _CEVT; }
            set { _CEVT = value; }
        }
     
      

        //private List<int> _NSS = new List<int>();
        ///// <summary>
        ///// 点状源个数
        ///// 需要被指定浓度的点状源.未指定的点状源浓度被默认为0.
        ///// </summary>
        //public List<int> NSS
        //{
        //    get { return _NSS; }
        //    set { _NSS = value; }
        //}
        private int _NSS;//点状源个数
        /// <summary>
        /// 点状源个数
        /// 需要被指定浓度的点状源.未指定的点状源浓度被默认为0.
        /// </summary>
        public int NSS
        {
            get { return _NSS; }
            set { _NSS = value; }
        }

        private List<List<PointConcentration>> _PointConcentration = new List<List<PointConcentration>>();
        /// <summary>
        /// 如果NSS>0,输入 NSS遍
        /// 点状源浓度,对于每个应力期来说
        /// </summary>
        public List<List<PointConcentration>> PointConcentration
        {
            get { return _PointConcentration; }
            set { _PointConcentration = value; }
        }
        


        public override void WriteFile(string path)
        {
            string content = string.Format("{0,2}", GetNumberLengthFormat( this.FWEL,2))  + string.Format("{0,2}",GetNumberLengthFormat(  this.FDRN,2))  + string.Format("{0,2}",GetNumberLengthFormat( this.FRCH,2))  +
                string.Format("{0,2}",GetNumberLengthFormat(  this.FEVT,2)) + string.Format("{0,2}",GetNumberLengthFormat(  this.FRIV,2))  + string.Format("{0,2}", GetNumberLengthFormat( this.FGHB,10)) +
                "      FWEL,FDRN,FRCH,FEVT,FRIV,FGHB" + "\r\n";
            content += string.Format("{0,10}", GetNumberLengthFormat( this.MXSS,10)) + "        MXSS" + "\r\n";
            

            for (int i = 0; i < this.INCRCH.Count; i++)
            {
                if (this.FRCH == "T")
                {
                    content += string.Format("{0,10}", GetNumberLengthFormat(this.INCRCH[i], 10)) + "\r\n";
                    for (int p = 0; p < this.CRCH.Count; p++)
                    {
                        string str = "";
                        if (this.INCRCH[i] >= 0)
                        {
                            if (IsConstant(this.CRCH[p][i]))
                            {
                                content += string.Format("{0,10}", GetNumberLengthFormat(0, 10)) + string.Format("{0,10}", GetNumberLengthFormat(this.CRCH[p][i][0, 0], 10)) + "                                          CRCH " + (i + 1).ToString() + "\r\n";
                            }
                            else
                            {
                                GetArrayString(this.CRCH[p][i], ref  str);
                                content += str;
                            }
                        }
                    }
                }
               
                if (this.FEVT == "T")
                {
                    content += string.Format("{0,10}", GetNumberLengthFormat(this.INCEVT[i], 10)) + "\r\n";
                }
                for (int p = 0; p < this.CEVT.Count; p++)
                {
                    string str = "";
                    if (this.FEVT == "T")
                    {
                        if (this.INCEVT[i] >= 0)
                        {
                            if (IsConstant(this.CEVT[p][i]))
                            {
                                content += string.Format("{0,10}", GetNumberLengthFormat(0, 10)) + string.Format("{0,10}", GetNumberLengthFormat(this.CEVT[p][i][0, 0], 10)) + "                                          CEVT " + (i + 1).ToString() + "\r\n";
                            }
                            else
                            {
                                GetArrayString(this.CEVT[p][i], ref  str);
                                content += str;
                            }
                        }
                    }
                }
             
                //if (this.FRCH == "T")
                //{
                //    content += string.Format("{0,10}", GetNumberLengthFormat(this.INCRCH[i], 10)) + "\r\n";
                //}
                //string str = "";
                //if (this.FRCH == "T")
                //{
                //    if (this.INCRCH[i] >= 0)
                //    {
                //        if (IsConstant(this.CRCH[i]))
                //        {
                //            content += string.Format("{0,10}", GetNumberLengthFormat(0, 10)) + string.Format("{0,10}", GetNumberLengthFormat(this.CRCH[i][0, 0], 10)) + "                                          CRCH " + (i + 1).ToString() + "\r\n";
                //        }
                //        else
                //        {
                //            GetArrayString(this.CRCH[i], ref  str);

                //            content += str;
                //        }
                //    }
                //}

                //if (this.FEVT == "T")
                //{
                //    content += string.Format("{0,10}", GetNumberLengthFormat(this.INCEVT[i], 10)) + "\r\n";
                //}
                //if (this.FEVT == "T")
                //{
                //    if (this.INCEVT[i] >= 0)
                //    {
                //        if (IsConstant(this.CEVT[i]))
                //        {
                //            content += string.Format("{0,10}", GetNumberLengthFormat(0, 10)) + string.Format("{0,10}", GetNumberLengthFormat(this.CEVT[i][0, 0], 10)) + "                                          CEVT " + (i + 1).ToString() + "\r\n";
                //        }
                //        else
                //        {
                //            GetArrayString(this.CEVT[i], ref  str);

                //            content += str;
                //        }
                //    }
                //}

                content += string.Format("{0,10}", GetNumberLengthFormat(this.NSS, 10)) + "        NSS" + "\r\n";
                if (this.NSS > 0)
                {
                    for (int k = 0; k < this.PointConcentration[i].Count; k++)
                    {
                        content += string.Format("{0,10}", GetNumberLengthFormat(this.PointConcentration[i][k].KSS, 10)) + string.Format("{0,10}", GetNumberLengthFormat(this.PointConcentration[i][k].ISS, 10)) +
                            string.Format("{0,10}", GetNumberLengthFormat(this.PointConcentration[i][k].JSS, 10)) + string.Format("{0,10}", GetNumberLengthFormat(this.PointConcentration[i][k].CSS, 10)) +
                            string.Format("{0,10}", GetNumberLengthFormat(this.PointConcentration[i][k].ITYPE, 10));

                        for (int j = 0; j < this.PointConcentration[i][k].CSSMS.Count; j++)
                        {
                            content += string.Format("{0,10}", GetNumberLengthFormat(this.PointConcentration[i][k].CSSMS[j], 10));
                        }
                        content += "      KSS,ISS,JSS,CSS,ITYPE" + "\r\n";
                    }
                }

            }

            string fname = path + this.Fname;
            HAUtil.FileOperations.WriteFile(fname, content);
        }
    }
    /// <summary>
    /// 如果NSS>0,输入 NSS遍
    /// </summary>
    public class PointConcentration
    {
        private int _KSS;//每个物种浓度的点状源的单元坐标,层
        /// <summary>
        /// 每个物种浓度的点状源的单元坐标，层
        /// </summary>
        public int  KSS
        {
            get { return _KSS; }
            set { _KSS = value; }
        }
        private int _ISS;
        /// <summary>
        /// 每个物种浓度的点状源的单元坐标，行
        /// </summary>
        public int ISS
        {
            get { return _ISS; }
            set { _ISS = value; }
        }
        private int _JSS;
        /// <summary>
        /// 每个物种浓度的点状源的单元坐标，列
        /// </summary>
        public int JSS
        {
            get { return _JSS; }
            set { _JSS = value; }
        }
        private double _CSS;
        /// <summary>
        /// 是在单物种模拟中指定的源浓度或质量装载率,取决于ITYPE 的值.
        /// (对于多物种模拟,不使用CSS,但是仍需要输入一个dummy value.)
        /// </summary>
        public double CSS
        {
            get { return _CSS; }
            set { _CSS = value; }
        }
        private int _ITYPE;
        /// <summary>
        /// 一个标示点状源种类的整数
        /// ITYPE=1,常水头单元;
        /// =2,井;
        /// =3,渠(注意在MODFLOW的惯例中,渠总是一个汇,因此如果是从MODFLOW得到的流体解渠的浓度不能被指定);
        /// =4,河(或溪);
        /// =5,一般水头依赖边界条件;
        /// =15,质量装载源;
        /// =-1,常浓度单元.
        /// </summary>
        public int ITYPE
        {
            get { return _ITYPE; }
            set { _ITYPE = value; }
        }

        private List<double> _CSSMS = new List<double>();
        /// <summary>
        /// 多物种模拟的点状源浓度
        /// </summary>
        public List<double> CSSMS
        {
            get { return _CSSMS; }
            set { _CSSMS = value; }
        }


     
    }
}
